class Solution(object):
    def containsNearbyDuplicate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: bool
        """
        a = {}
        for i in range(len(nums)):
            if nums[i] not in a:
                a[nums[i]] = i
            else:
                if abs(a[nums[i]] - i) < k:
                    return True
                else:
                    a[nums[i]] = i
        return False


m =[1,2,3,1]
s = Solution()
a=  s.containsNearbyDuplicate(m,3)
print(a)